﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    ///  SELECT p.* from ICC_VKPRICE p WHERE   not  EXISTS (SELECT 1 FROM vkpos x WHERE P.VKPAUFKRS=X.VKPAUFKRS AND P.VKPAUFLFD=X.VKPAUFLFD and p.vkpart=x.vkpart)
    /// </summary>
    public partial class COWSEDIT : DocumentBase
    {
        #region-------------Properties-----------------------
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
      
        string rownid = "";
        bool IsLoaded = false;
        DataRow CurrentRow = null;
        DataSet SResult = null;
        DataRow LastRow = null;
        DataSet SetMap = null;
    
        public DataSet dsResult;
        MainViewModel vm;

        ITXEXRPT ITXEXRPT;
        DataTable tbUser = null;
        public DataSet SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
        }

        public DataSet SetData
        {
            get
            {
                return vm.SetData;
            }
        }
        public DataSet SetProd
        {
            get
            {
                return vm.SetProd;
            }
        }
        CollectionViewSource vwVKP
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwVKP");
            }
        }
        DataRow dwVKP
        {
            get
            {
                if (vwVKP.View == null)
                    return null;
                if ((DataRowView)vwVKP.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKP.View.CurrentItem).Row;
            }
        } 
         
        string user = "";
        #endregion
        public COWSEDIT()
        {
            InitializeComponent(); 
        } 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (vm == null)
                    vm = DataContext as MainViewModel;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }  
     
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet SResult = null;
                 SResult = vm.PVK.QueryICC_VKPRICE(null, 5);
                SetData.Clear();
                SetData.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwVKP == null)
                    return;
                vm.PVK.DeleteICC_VKPRICE(dwVKP, 1);
                string rownid = dwVKP["ROWNID"].ToString();
                vwVKP.View.MoveCurrentTo(-1);
                string filter = "ROWNID=" + rownid + "";
                DataRow[] rows = SetData.Tables["ICC_VKPRICE"].Select(filter);
                foreach (DataRow row in rows)
                {
                    row.Delete();
                }
                SetData.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }  
    }
}
